package com.ruoyi.record.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.staff.domain.Staff;
import com.ruoyi.staff.mapper.StaffMapper;
import com.ruoyi.staff.service.IStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.record.mapper.RecordMapper;
import com.ruoyi.record.domain.Record;
import com.ruoyi.record.service.IRecordService;

/**
 * 问诊记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-24
 */
@Service
public class RecordServiceImpl implements IRecordService {
    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private IStaffService staffService;

    /**
     * 查询问诊记录
     *
     * @param id 问诊记录主键
     * @return 问诊记录
     */
    @Override
    public Record selectRecordById(Long id) {
        Record record = recordMapper.selectRecordById(id);
        //填入医生姓名
        Long staffId = record.getDoctorId();
        if (staffId != null) {
            Staff staff = staffMapper.selectStaffById(staffId);
            record.setStaffName(staff.getName());
        }
        return record;
    }

    /**
     * 查询问诊记录列表
     *
     * @param record 问诊记录
     * @return 问诊记录
     */
    @Override
    public List<Record> selectRecordList(Record record) {
        QueryWrapper<Record> wrapper = new QueryWrapper<>();
        wrapper.lambda();
        //根据角色展示不同的信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysRole> roles = loginUser.getUser().getRoles();
        Long roleId = roles.get(0).getRoleId();
        Long userId = loginUser.getUserId();
        //病人，只能查自己的问诊记录
        if (roleId.equals(100L)) {
            wrapper.eq("patient_id", userId);
        }
        //医生，只能查自己的问诊记录
        if (roleId.equals(101L)) {
            Staff staff = staffService.selectStaffByUserId(userId);
            wrapper.eq("doctor_id", staff.getId());
        }

        //条件查询
        Object beginCreateTime = record.getParams().get("beginCreateTime");
        Object endCreateTime = record.getParams().get("endCreateTime");
        if (beginCreateTime != null && endCreateTime != null) {
            wrapper.between("create_time", beginCreateTime, endCreateTime);
        }
        if (record.getName() != null) {
            wrapper.like("name", record.getName());
        }
        wrapper.orderByDesc("create_time");
        List<Record> records = recordMapper.selectList(wrapper);

        //填入医生姓名
        for (Record r : records) {
            Long staffId = r.getDoctorId();
            if (staffId != null) {
                Staff staff = staffMapper.selectStaffById(staffId);
                r.setStaffName(staff.getName());
            }
        }
        return records;
    }

    /**
     * 新增问诊记录
     *
     * @param record 问诊记录
     * @return 结果
     */
    @Override
    public int insertRecord(Record record) {
        //判断当前角色
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysRole> roles = loginUser.getUser().getRoles();
        Long roleId = roles.get(0).getRoleId();
        //如果是医生，新增的问诊记录都是当前医生的数据（忽略前端的选择）
        if (roleId.equals(101L)) {
            Long staffUserId = loginUser.getUserId();
            QueryWrapper<Staff> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", staffUserId);
            Staff staff = staffMapper.selectOne(wrapper);
            record.setDoctorId(staff.getId());
        }
        //设置创建时间
        record.setCreateTime(DateUtils.getNowDate());
        //设置病人ID
//        Long userId = SecurityUtils.getUserId();
//        record.setPatientId(userId);
        return recordMapper.insertRecord(record);
    }

    /**
     * 修改问诊记录
     *
     * @param record 问诊记录
     * @return 结果
     */
    @Override
    public int updateRecord(Record record) {
        record.setUpdateTime(DateUtils.getNowDate());
        return recordMapper.updateRecord(record);
    }

    /**
     * 批量删除问诊记录
     *
     * @param ids 需要删除的问诊记录主键
     * @return 结果
     */
    @Override
    public int deleteRecordByIds(Long[] ids) {
        return recordMapper.deleteRecordByIds(ids);
    }

    /**
     * 删除问诊记录信息
     *
     * @param id 问诊记录主键
     * @return 结果
     */
    @Override
    public int deleteRecordById(Long id) {
        return recordMapper.deleteRecordById(id);
    }
}
